#include "SFMLCamera.h"
#include "..\Define.h"
#include "..\ScaleFactor.h"

using namespace vn;

SFMLCamera::SFMLCamera(sf::View* p_view) : m_view(p_view) {}


SFMLCamera::~SFMLCamera(void){}

void SFMLCamera::update()
{
	float x = m_target.x - m_position.x;
	float y = m_target.y - m_position.y;

	if ((x*x + y*y) <= 1.0f)
	{
		m_position = m_target;
	}
	else
	{
		float d = sqrtf( (x*x + y*y) );
		float v = (d * m_velocity) / 60.0f;
		//if (v < 1.0f)
		//	v = 1.0f;
		float vx = x * (v/d);
		float vy = y * (v/d);

		m_position.x += vx;
		m_position.y += vy;
	}

	m_view->setCenter(m_position.x * SCALEF, m_position.y * SCALEF);
	m_boundary = vn::FloatRect(m_position.x - VSW_05, m_position.y - VSH_05, m_cameraSize.w, m_cameraSize.h);

	if (!m_freeMovement)
	{
		if (m_position.x < VSW_05)
			m_position.x = VSW_05;
		else if (m_position.x > (m_levelSize.w - VSW_05))
			m_position.x = (m_levelSize.w - VSW_05);
		if (m_position.y < VSH_05)
			m_position.y = VSH_05;
		else if (m_position.y > (m_levelSize.h - VSH_05))
			m_position.y = (m_levelSize.h - VSH_05);
	}
}